<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="close">
    <!-- 放置表单内容 -->
    <el-form ref="addForm" :model="addForm" :rules="addRules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model.trim="addForm.name" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model.trim="addForm.code" style="width: 80%;" size="mini" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="addForm.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
          <!-- 下拉选项 循环 负责人数据 label表示显示的字段 value 存储字段 -->
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model.trim="addForm.introduce"
          style="width: 80%;"
          size="mini"
          type="textarea"
          :rows="4"
          placeholder="1-100个字符"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-row style="width:100%;" type="flex" justify="end">
        <el-button size="mini" type="text" style="color: #727479;margin-right:20px" @click="close">取消</el-button>
        <el-button type="primary" size="mini" @click="btnOK">确认</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartmentAPI, getUserListAPI, addDeptAPI, getDepartMentDetailAPI, updateDepartMentAPI } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],
      addForm: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      addRules: {
        code: [
          { required: true, message: '编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '编码为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const list = await getDepartmentAPI()
              if (this.addForm.id) {
                list.filter(item => item.id !== this.addForm.id).some(item => item.code === value) ? callback(new Error(`当前部门已有${value}编码`)) : callback()
              } else {
                list.some(item => item.code === value) ? callback(new Error(`当前部门已有${value}编码`)) : callback()
              }
            },
            trigger: 'blur'
          }
        ],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '部门介绍为1-100个字符', trigger: 'blur' }
        ],
        managerId: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              const list = await getDepartmentAPI()
              if (this.addForm.id) {
                list.filter(item => item.id !== this.addForm.id).some(item => item.name === value) ? callback(new Error(`当前部门已有${value}名称`)) : callback()
              } else {
                list.some(item => item.name === value) ? callback(new Error(`当前部门已有${value}名称`)) : callback()
              }
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.addForm.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    close() {
      this.addForm = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
      this.$emit('update:showDialog', false)
    },
    async btnOK() {
      this.$refs.addForm.validate()
      let msg = '新增'
      if (this.addForm.id) {
        await updateDepartMentAPI(this.addForm)
        msg = '编辑'
      } else {
        await addDeptAPI({ ...this.addForm, pid: this.currentNodeId })
      }
      this.$message.success(`${msg}部门成功`)
      this.$emit('updateDept')
      this.close()
    },
    async getUserList() {
      this.managerList = await getUserListAPI()
    },
    async getDepartMentDetail(id) {
      this.addForm = await getDepartMentDetailAPI(id)
    }
  }
}
</script>

<style>
</style>
